package com.lu.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @program lu-boot-saas
 * @description: redis配置
 * @author: zhanglu
 * @create: 2022-09-26 14:25:00
 */
@Configuration
public class RedisConfig {

  @Value("${spring.redis.host}")
  private String hostName;
  @Value("${spring.redis.port}")
  private Integer port;
  @Value("${spring.redis.password}")
  private String password;

  @Bean
  @ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
  public GenericObjectPoolConfig redisPool(){
    return new GenericObjectPoolConfig();
  }

  @Bean
  public StringRedisTemplate stringRedisTemplate(){
    return new StringRedisTemplate(redisFactory(2));
  }

  /**
   * redis 1号库 配置策略 (代码执行策略)
   *
   * @return
   */
  @Bean
  public RedisTemplate<String, Object> redisTemplateStrategy() {
    return redisTemplate(1);
  }

  /**
   * redis 2号库 分布式锁，明幂等性等 (代码调度控制)
   *
   * @return
   */
  @Bean
  public RedisTemplate<String, Object> redisTemplateLock() {
    return redisTemplate(2);
  }

  /**
   * 缓存
   * @return
   */
  @Bean
  public RedisTemplate<String, Object> redisTemplate() {
    return redisTemplate(3);
  }

  /**
   * 缓存
   * @return
   */
  @Bean
  public RedisTemplate<String, Object> redisTemplate3() {
    return redisTemplate(3);
  }

  /**
   * 缓存
   * @return
   */
  @Bean
  @SuppressWarnings("all")
  public RedisTemplate<String, Object> redisTemplate4() {
    return redisTemplate(4);
  }

  /**
   * 场景模块缓存
   * @return
   */
  @Bean
  public RedisTemplate<String, Object> redisTemplate7() {
    return redisTemplate(7);
  }

  public RedisTemplate<String, Object> redisTemplate(int database) {
    RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
    template.setConnectionFactory(redisFactory(database));
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(
        Object.class);
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);
    StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    // key采用String的序列化方式
    template.setKeySerializer(stringRedisSerializer);
    // hash的key也采用String的序列化方式
    template.setHashKeySerializer(stringRedisSerializer);
    // value序列化方式采用jackson
    template.setValueSerializer(jackson2JsonRedisSerializer);
    // hash的value序列化方式采用jackson
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
    template.afterPropertiesSet();
    return template;
  }

  public LettuceConnectionFactory redisFactory(int database) {
    RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
    configuration.setHostName(hostName);
    configuration.setPort(port);
    configuration.setPassword(password);
    configuration.setDatabase(database);
    GenericObjectPoolConfig poolConfig = redisPool();
    LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(configuration,
        LettucePoolingClientConfiguration.builder().poolConfig(poolConfig).build());
    lettuceConnectionFactory.afterPropertiesSet();
    return lettuceConnectionFactory;
  }

}
